# Replication files for:
# "Generalizability of Heterogeneous Treatment Effect Estimates Across Samples"
# Alexander Coppock, Thomas J. Leeper, and Kevin J. Mullinix
# Proceedings of the National Academy of Sciences, Forthcoming

rm(list = ls())
library(dplyr)
library(readr)
library(xtable)

results <- read_rds(path = "CLM_results.rds")
results <- 
  results %>%
  group_by(study, sample, covariate) %>%
  mutate(n = df + 2, prop = (n) / sum(n), n = as.character(n))

group <- c(
  "age_3_18 - 39",
  "age_3_40 - 59",
  "age_3_More than 60",
  "pid_3_Democrat",
  "pid_3_Independent",
  "pid_3_Republican",
  "white_1",
  "white_0",
  "educ_3_Less than College",
  "educ_3_College",
  "educ_3_Graduate School",
  "female_0",
  "female_1",
  "ideo_3_Liberal",
  "ideo_3_Moderate",
  "ideo_3_Conservative"
)
group_fac_labels <- c(
  "Age: 18 - 39",
  "Age: 40 - 59",
  "Age: More than 60",
  "Party ID: Democrat",
  "Party ID: Independent",
  "Party ID: Republican",
  "Race: White",
  "Race: Nonwhite",
  "Education: Less than College",
  "Education: College",
  "Education: Graduate School",
  "Gender: Male",
  "Gender: Female",
  "Ideology: Liberal",
  "Ideology: Moderate",
  "Ideology: Conservative"
)

group_fac <- factor(group, levels = group, labels = group_fac_labels)

labels_df <- data.frame(group, group_fac)

results <- left_join(results, labels_df)

format_num <- function(x, digits=3){
  x <- as.numeric(x)
  return(paste0(sprintf(paste0("%.", digits, "f"), x)))
}

make_ci <- function(ci_lower, ci_upper, digits = 2) {
  paste0("[",
         format_num(ci_lower, digits),
         ", ",
         format_num(ci_upper, digits),
         "]")
}

appendix_df <-
  results %>%
  group_by(study, study_label) %>%
  summarize(n()) %>%
  arrange(study_label)

results_tables <- results %>% ungroup %>% filter(term == "Z") %>% arrange(group_fac) %>%
  transmute(study_label, sample, group_fac, estimate, std.error, p.value, ci = make_ci(conf.low, conf.high), n, prop) 

sink("appendix_tables.tex")
for (i in 1:nrow(appendix_df)) {
  original <-
  results_tables %>%
    filter(study_label == appendix_df$study_label[i], sample == "original") %>%
    select(-study_label,-sample) %>%
    xtable() %>%
    print.xtable(
      include.colnames = FALSE,
      include.rownames = FALSE,
      only.contents = TRUE,
      print.results = FALSE,
      comment = FALSE,
      hline.after = c())
  mt <- 
  results_tables %>%
      filter(study_label == appendix_df$study_label[i], sample == "mt") %>%
      select(-study_label,-sample) %>%
      xtable() %>%
      print.xtable(
        include.colnames = FALSE,
        include.rownames = FALSE,
        only.contents = TRUE,
        print.results = FALSE,
        comment = FALSE,
        hline.after = c())
  
  cat(
    paste0(
      "\\begin{table}
      \\centering
      \\caption{",
      appendix_df$study_label[i],
      " Original and Replication Results}
      \\begin{tabular}{lrrrrrr}
      \\toprule
      \\multicolumn{7}{c}{Original}\\\\
      Covariate Class & CATE & SE & p & 95\\% CI & N & Prop\\\\ \\midrule",
      original,
      "\\midrule
      \\multicolumn{7}{c}{Mechanical Turk}\\\\
      Covariate Class & CATE & SE & p & 95\\% CI & N & Prop\\\\ \\midrule",
      mt,
      "\\bottomrule
      \\end{tabular}
      \\end{table}
      
      \\clearpage
      "
    )
    )
  }
sink()





